#include "mediadatapicker.h"

#include <QCoreApplication>
#include <QMediaMetaData>
#include <QCryptographicHash>
#include <QPixmap>
#include <QDebug>


MediaDataPicker::MediaDataPicker(QObject *parent) : QObject(parent)
{
    const QString dirName = QString("%1%2%3")
                            .arg(qApp->applicationName())
                            .arg( QDir::separator())
                            .arg(QLatin1String("images"));
    m_coverDir = QDir::temp();
    m_coverDir.mkpath(dirName);
    m_coverDir.cd(dirName);
}

QObject *MediaDataPicker::player()
{
    return m_player;
}

void MediaDataPicker::setPlayer(QObject *qmlMediaPlayer)
{
    QMediaPlayer *player = qvariant_cast<QMediaPlayer *>(qmlMediaPlayer->property("mediaObject"));
    if(!player || m_player == player) {
        return;
    }

    if(m_player) {
        disconnect(m_player, 0, 0, 0);
    }

    m_player = player;

    connect(m_player, SIGNAL(metaDataChanged(QString, QVariant)), SLOT(onMetaDataChanged(QString, QVariant)));
    connect(m_player, SIGNAL(mediaChanged(QMediaContent)), SLOT(reset()));
}

QString MediaDataPicker::title() const
{
    if(!m_player) {
        return QString();
    }
    return m_player->metaData(QMediaMetaData::Title).toString();
}
QString MediaDataPicker::artist() const
{
    if(!m_player) {
        return QString();
    }
    return m_player->metaData(QMediaMetaData::ContributingArtist).toString();
}
QString MediaDataPicker::albumTitle() const
{
    if(!m_player) {
        return QString();
    }
    return m_player->metaData(QMediaMetaData::AlbumTitle).toString();
}
QString MediaDataPicker::albumArtist() const
{
    if(!m_player) {
        return QString();
    }
    return m_player->metaData(QMediaMetaData::AlbumArtist).toString();
}
QUrl MediaDataPicker::coverArtUrl() const
{
    return m_coverArtUrl;
}
int MediaDataPicker::trackNumber() const
{
    if(!m_player) {
        return -1;
    }
    return m_player->metaData(QMediaMetaData::TrackNumber).toInt();
}

void MediaDataPicker::onMetaDataChanged(const QString &key, const QVariant &value)
{
    qDebug("%s = %s", qPrintable(key), qUtf8Printable(value.toString()));
    if(key == QLatin1String("AlbumTitle")) {
        emit albumTitleChanged();
    } else if(key == QLatin1String("AlbumArtist")) {
        emit albumArtistChanged();
    } else if(key == QLatin1String("Title")) {
        emit titleChanged();
    } else if(key == QLatin1String("ContributingArtist")) {
        emit artistChanged();
    } else if(key == QLatin1String("TrackNumber")) {
        emit trackNumberChanged();
    } else if(key == QLatin1String("image")) {
        QCryptographicHash md5(QCryptographicHash::Md5);
        md5.addData(m_player->media().canonicalUrl().toEncoded());
        const QString fileName = QString("%1%2")
                                 .arg(QString::fromLocal8Bit(md5.result().toHex()))
                                 .arg(QLatin1String(".jpg"));
        m_coverArtUrl = QUrl::fromLocalFile(m_coverDir.absoluteFilePath(fileName));
        if(!m_coverDir.exists(fileName)) {
            QPixmap image = QPixmap::fromImage(m_player->metaData("image").value<QImage>());
            image.save(m_coverArtUrl.toLocalFile());
        }
        emit coverArtUrlChanged();
    }
}

void MediaDataPicker::classBegin()
{

}

void MediaDataPicker::componentComplete()
{
    m_componentComplete = true;
    this->reset();
}

void MediaDataPicker::reset()
{
    emit albumTitleChanged();
    emit albumArtistChanged();
    emit titleChanged();
    emit artistChanged();
    emit trackNumberChanged();
    m_coverArtUrl.clear();
    emit coverArtUrlChanged();
}
